﻿using System;
using Microsoft.EntityFrameworkCore;
using Model;
using Model.eums;
using Model.Model;

namespace Utils.EFInit;

public class UserDbService
{
    public static async void UserDbInit()
    {
        await using var db = new DataDbContent();
        try
        {
            var use = await db.LoginUser!.FirstOrDefaultAsync(e => e.Account == "admin");
            if (use is null)
                throw new Exception("Please Create Db");
        }
        catch (Exception)
        {
            await db.Database.EnsureCreatedAsync();
            await db.LoginUser!.AddAsync(new LoginModel
            {
                UserName = "XIOA",
                Account = "admin",
                Password = "2873",
                CreateDateTime = DateTime.Now,
                Auth = Auth.Admin
            });
            await db.SaveChangesAsync();
        }
    }
}